package server

import (
	"net/http"
)

type server struct {
	addr        string
	handlerFunc middleware
}

func New(addr string, mid ...middleware) *server {
	var handlerFunc middleware
	if len(mid) > 0 {
		handlerFunc = mid[0]
	} else {
		handlerFunc = default_middleware
	}
	return &server{
		addr:        addr,
		handlerFunc: handlerFunc,
	}
}

func (s *server) AddRouter(path string, http_func http.HandlerFunc) {
	if s.handlerFunc != nil {
		http.HandleFunc(path, s.handlerFunc(http_func))
	} else {
		http.HandleFunc(path, default_middleware(http_func))
	}
}

func (s *server) Run() error {
	return http.ListenAndServe(s.addr, nil)
}
